<?php

namespace app\models;

use yii\base\Exception;
use yii\db\ActiveRecord;

/**
 * Class BaseModel
 * @package common\models
 *
 * @property array $attachment
 */
class BaseModel extends ActiveRecord {
    protected static $version = 'v0';
    protected static function version() {
        return static::$version;
    }

    public function save($runValidation = true, $attributeNames = null, $throwException = true) {
        $result = parent::save($runValidation, $attributeNames);
        if(!$result && $throwException) {
            throw new Exception(print_r($this->errors, true));
        }
        return $result;
    }

    // load数组，但是先过滤一下
    public function loadBeforeFilter($data, $standardFields = [], $formName = '') {
        return $this->load(self::filter($data, $standardFields), $formName);
    }

    public static function filter($array, $filters = []) {
        $result = [];
        foreach ((array)$filters as $globalKey) {
            if(isset($array[$globalKey])) {
                $result[$globalKey] = $array[$globalKey];
            }
        }
        return $result;
    }
}
